package com.example.receiver;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.activity.MessageInfoActivity;
import com.example.fragment.IMainActivity;
import com.example.tools.Tools;
import cn.jpush.android.api.JPushInterface;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class JpushReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String content = intent.getStringExtra(JPushInterface.EXTRA_EXTRA);
		JSONObject objJson;
		String key = "url";
		String url = "";
		try {
			objJson = new JSONObject(content);
			key = "url";
			url = objJson.getString(key);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (url != null && !url.equals("") && url.contains("http")) {
			if (Tools.isAppInForeground(context)) {
				intent = new Intent(context.getApplicationContext(),
						MessageInfoActivity.class);
			} else {
				intent = new Intent(context.getApplicationContext(),
						IMainActivity.class);
			}
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra(key, url);
			context.getApplicationContext().startActivity(intent);
		} else {
			if (!Tools.isAppInForeground(context)) {
				intent = new Intent(context.getApplicationContext(),
						IMainActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.getApplicationContext().startActivity(intent);
			}
		}
	}
}
